#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright (c) 2025 粤港澳大湾区（广东）国创中心

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

File: pyncti_find_small_feature.py
Desc: Function to find small features in selected objects based on user-defined criteria.
Author: xumanjia
Date: 2025-08-01
"""
def pyncti_find_small_feature(NCTI,doc):
    doc.ResetCaseResult()
    sel = NCTI.SelectionManager(doc)
    if len(sel.ObjectNames) == 0:
        print(f"请先选择几何对象")
        return
    res  = doc.ReturnDialogData(-1, "查找小特征", "查找类型：", ["体", "面"], "阈值：", "0.05")
    if len(res) == 2:
        type = 0
        tolerance = float(res[1])
        if res[0][0] == "面":
            type = 1
        features = doc.FindSmallFeatures(sel.ObjectNames, type, tolerance)
        sel.ClearSelected()
        if  features != None:
            if type == 0:
                sel.ObjectNames = features
                print(f"共查找到{len(features)}个体特征")
            else:
                objNames = []
                cellIDs = []
                for key, value in features.items():
                    for id in value:
                        objNames.append(key)
                        cellIDs.append(id)
                sel.ObjectNames = objNames
                sel.CellIDs = cellIDs
                print(f"共查找到{len(cellIDs)}个面特征")
            sel.SetSelected()
            doc.ShowFeaturesDialog(type, features)